package no.progark.views;

import no.progark.copter.Copter;
import no.progark.copter.Direction;
import no.progark.helikopter.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.View;

public class CopterView extends View {
	MediaPlayer sound;
	//lying down (direction left/right)
	public final int HELIHEIGHT = 22; //54;
	public final int HELIWIDTH = 28; //59;
	public final int HELISPEED = 4;//4;
	public final int PLAYERSPEED = 4;//4;
	public int touchX = 0;
	public int touchY = 0;
	public int maxHeight;
	public int maxWidth;
	public long prevMS = System.currentTimeMillis(); //Time of last draw
	public long currentMS = 0; //Current time difference from last draw
	public long second = 0; //Times one second, used for calculating frames per second.
	public int oldFPS = 0; // The one that is painted
	public int currentFPS = 0; //We calculate this untill a second has passed, then set value to old. paint old value for One whole second.
	public long dt = 0;
	public long animationrate = 100;
	//Gray helicopter
	public Paint paint = new Paint();
	public Copter player = new Copter(Direction.DOWN, false, 200, 360, R.drawable.copterleft, R.drawable.copterright, R.drawable.copterup, R.drawable.copterdown);
	public Bitmap playerimage = BitmapFactory.decodeResource(getResources(), player.initImage());
	boolean init = true;
	//Blue helicopter
	public Copter copter1 = new Copter(Direction.LEFT, false, 200, 360, R.drawable.yellowleft, R.drawable.yellowright, R.drawable.yellowup, R.drawable.yellowdown);
	public Bitmap copter1image = BitmapFactory.decodeResource(getResources(), copter1.initImage());
	//Green helicopter
	public Copter copter2 = new Copter(Direction.UP, false, 200, 360,  R.drawable.yellowleft, R.drawable.yellowright, R.drawable.yellowup, R.drawable.yellowdown);
	public Bitmap copter2image = BitmapFactory.decodeResource(getResources(), copter2.initImage());

	
 
	public CopterView(Context context) {
		super(context); 
		playSound();
		System.out.println("initiating");
		
		player.setImages(R.drawable.copterleft2, R.drawable.copterright2, R.drawable.copterup2, R.drawable.copterdown2);
		copter1.setImages(R.drawable.yellowleft2, R.drawable.yellowright2, R.drawable.yellowup2, R.drawable.yellowdown2);
		copter2.setImages(R.drawable.yellowleft2, R.drawable.yellowright2, R.drawable.yellowup2, R.drawable.yellowdown2);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		calcFPS();
		
		//Set background
		Rect bcg = new Rect();
		bcg.set(0, 0, canvas.getWidth(), canvas.getHeight());
		
		//Make paint
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setStyle(Paint.Style.FILL);
		
		//Render background with paint
		canvas.drawRect(bcg, paint);
		maxHeight = this.getMeasuredHeight();
		maxWidth = this.getMeasuredWidth();
		//Draw!
		drawCopter(playerimage, player.getX(), player.getY(), canvas);
		drawCopter(copter1image, copter1.getX(), copter1.getY(), canvas);
		drawCopter(copter2image, copter2.getX(), copter2.getY(), canvas);
		
		drawPositionCoordinates(player.getX(), player.getY(), canvas);
//		drawInfo(touchX, touchY, 40, 50, canvas);
//		drawInfo(maxWidth, maxHeight, 100, 50, canvas);
		drawFPS(oldFPS, 20, 50, canvas);
		bounceCollision();
		
		if(!init)playerimage = update(canvas, player, playerimage);//Counter(canvas);
		copter1image = update(canvas, copter1, copter1image);
		copter2image = update(canvas, copter2, copter2image);
		//Restart method
		if(dt > animationrate)dt=0;
		invalidate();
	}
	
	private void calcFPS() {
		long tempTime = System.currentTimeMillis();
		currentMS = tempTime - prevMS;
		prevMS = tempTime;
		second += currentMS;
		if(second < 1000)currentFPS++;
		else{
			second = 0;
			oldFPS = currentFPS;
			currentFPS = 0;
		}
		dt+=currentMS;
	}

	private void drawCopter(Bitmap copter, int x, int y, Canvas c){
		c.drawBitmap(copter, x, y, paint);
	}
	
	public void drawPositionCoordinates(int x, int y, Canvas c){

		
		player.drawCoords(c, 30, maxHeight-5, Color.RED);
		copter1.drawCoords(c, 100,maxHeight-5, Color.YELLOW);
		copter2.drawCoords(c, 170, maxHeight-5, Color.YELLOW);
	}
	
	public void bounceCollision(){
		if(Math.abs(copter1.getX()-player.getX()) < HELIHEIGHT && Math.abs(copter1.getY()-player.getY()) < HELIHEIGHT){
			player.setCollision(true);
			copter1.setCollision(true);
		}
		if(Math.abs(copter1.getX()-copter2.getX()) < HELIHEIGHT && Math.abs(copter1.getY()-copter2.getY()) < HELIHEIGHT){
			copter1.setCollision(true);
			copter2.setCollision(true);
		}
		if(Math.abs(player.getX()-copter2.getX()) < HELIHEIGHT && Math.abs(player.getY()-copter2.getY()) < HELIHEIGHT){
			player.setCollision(true);
			copter2.setCollision(true);
		}
	}
	
	public Bitmap update(Canvas canvas, Copter copter, Bitmap copterimage){ 
		if(!sound.isPlaying())sound.start();
		if(copter.getDir()==Direction.RIGHT){
			if(copter.getX()+HELIWIDTH >= maxWidth || copter.isCollision()){
				copter.setDir(copter.getDir().getOppositeDirection());
				copter.imageID = copter.initImage(); //WORKS?
				copterimage = BitmapFactory.decodeResource(getResources(), copter.imageID); 
				copter.setX(copter.getX()-HELIWIDTH);
				copter.setCollision(false);
			}
			else{
				
				if(dt>animationrate){
					copterimage = BitmapFactory.decodeResource(getResources(), copter.getNextImage());
					
				}
				copter.setX(copter.getX()+HELISPEED);
			}
		}
		else if(copter.getDir()==Direction.LEFT){
			if(copter.getX()-5 < 0 || copter.isCollision()){
				copter.setDir(copter.getDir().getOppositeDirection());
				copter.imageID = copter.initImage();
				copterimage = BitmapFactory.decodeResource(getResources(), copter.imageID);
				copter.setX(copter.getX()+HELIWIDTH);
				copter.setCollision(false);
			}
			else{
				if(dt>animationrate){
					copterimage = BitmapFactory.decodeResource(getResources(), copter.getNextImage());
					
				}
				copter.setX(copter.getX()-HELISPEED);
			}
		}
		if(copter.getDir()==Direction.DOWN){
			if(copter.getY()+HELIWIDTH > maxHeight || copter.isCollision()){
				copter.setDir(copter.getDir().getOppositeDirection());
				copter.imageID = copter.initImage();
				copterimage = BitmapFactory.decodeResource(getResources(), copter.imageID); 
				copter.setY(copter.getY()-HELIWIDTH);
				copter.setCollision(false);
			}
			else{
				if(dt>animationrate){
					copterimage = BitmapFactory.decodeResource(getResources(), copter.getNextImage());
					
				}
				copter.setY(copter.getY()+HELISPEED);;
			}
		}
		else if(copter.getDir()==Direction.UP){
			if(copter.getY()-5 < 0|| copter.isCollision()){
				copter.setDir(copter.getDir().getOppositeDirection());
				copter.imageID = copter.initImage();
				copterimage = BitmapFactory.decodeResource(getResources(), copter.imageID); 
				copter.setY(copter.getY()+HELIWIDTH);
				copter.setCollision(false);
			}
			else{
				if(dt>animationrate){
					copterimage = BitmapFactory.decodeResource(getResources(), copter.getNextImage());
					
				}
				copter.setY(copter.getY()-HELISPEED);
			}
		}
		return copterimage;
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		onTouchChangeDirection(event.getX(), event.getY());
		return super.onTouchEvent(event);
	}
	
	public void playSound(){
		sound = MediaPlayer.create(getContext(), R.raw.heli_sound);
	}
	
	public void drawInfo(int x1, int y1, int x2, int y2, Canvas c){
		Paint textPaint = new Paint();
		textPaint.setColor(Color.BLACK);
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setTextSize(10);
		c.drawText(""+x1+","+y1+"", x2, y2, textPaint);
	}
	
	public void drawFPS(int fps, int posx, int posy, Canvas c){
		Paint textPaint = new Paint();
		textPaint.setColor(Color.YELLOW);
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setTextSize(10);
		c.drawText("FPS: " + fps + "", posx, posy, textPaint);
	}
	
	
	public void onTouchChangeDirection(float xcoord, float ycoord){
		float dx = xcoord-player.getX();
		float dy = ycoord-player.getY();
		touchX = (int)xcoord;
		touchY = (int)ycoord;
		if(Math.abs(dx)>Math.abs(dy)){
			player.setDir((dx>0)?Direction.RIGHT:Direction.LEFT);
			System.out.println("Direction Changed caused by touch, new direction: " + player.getDir());
		}
		else{
			player.setDir((dy>0)?Direction.DOWN:Direction.UP);
			System.out.println("Direction Changed caused by touch, new direction: " + player.getDir());
		}
		init = false;
		player.imageID = player.initImage();
		playerimage = BitmapFactory.decodeResource(getResources(), player.initImage());
	}

}
